package com.ozo.cloud.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ozo.cloud.common.base.constant.StatusEnum;
import com.ozo.cloud.common.base.model.BaseBatchDTO;
import com.ozo.cloud.common.core.exception.ServiceException;
import com.ozo.cloud.common.database.util.SortUtils;
import com.ozo.cloud.system.exception.SystemExceptionEnum;
import com.ozo.cloud.system.pojo.dto.SysDictTypeQueryDTO;
import com.ozo.cloud.system.pojo.dto.SysDictTypeSaveDTO;
import com.ozo.cloud.system.entity.SysDictData;
import com.ozo.cloud.system.entity.SysDictType;
import com.ozo.cloud.system.mapper.SysDictDataMapper;
import com.ozo.cloud.system.mapper.SysDictTypeMapper;
import com.ozo.cloud.system.service.ISysDictTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 字典类型 服务实现类
 * </p>
 *
 * @author qiangesoft
 * @since 2024-07-11
 */
@RequiredArgsConstructor
@Service
public class SysDictTypeServiceImpl extends ServiceImpl<SysDictTypeMapper, SysDictType> implements ISysDictTypeService {

    private final SysDictDataMapper sysDictDataMapper;

    @Override
    public void add(SysDictTypeSaveDTO saveDTO) {
        // 参数校验
        this.checkParam(null, saveDTO.getDictCode());

        SysDictType sysDictType = BeanUtil.copyProperties(saveDTO, SysDictType.class);
        baseMapper.insert(sysDictType);
    }

    /**
     * 合法性校验
     *
     * @param id
     * @param dictCode
     */
    private void checkParam(Long id, String dictCode) {
        LambdaQueryWrapper<SysDictType> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysDictType::getDictCode, dictCode)
                .ne(Objects.nonNull(id), SysDictType::getId, id);
        Long count = baseMapper.selectCount(queryWrapper);
        if (count > 0) {
            throw new ServiceException(SystemExceptionEnum.DICT_CODE_EXISTS);
        }
    }

    @Override
    public void remove(BaseBatchDTO<Long> batchDTO) {
        baseMapper.deleteBatchIds(batchDTO.getIdList());
    }

    @Override
    public void edit(SysDictTypeSaveDTO saveDTO) {
        SysDictType sysDictType = BeanUtil.copyProperties(saveDTO, SysDictType.class);
        baseMapper.updateById(sysDictType);
    }

    @Override
    public List<SysDictType> list(SysDictTypeQueryDTO queryDTO) {
        QueryWrapper<SysDictType> queryWrapper = new QueryWrapper<>();
        // 排序
        String order = queryDTO.getOrder();
        if (StrUtil.isBlank(order)) {
            queryWrapper.lambda().orderByAsc(SysDictType::getId);
        } else {
            SortUtils.setSortCondition(queryWrapper, queryDTO.getProp(), order);
        }
        // 条件
        queryWrapper.lambda()
                .like(StrUtil.isNotBlank(queryDTO.getDictName()), SysDictType::getDictName, queryDTO.getDictName())
                .eq(StrUtil.isNotBlank(queryDTO.getDictCode()), SysDictType::getDictCode, queryDTO.getDictCode());
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public List<SysDictData> getDataByCode(String dictCode) {
        LambdaQueryWrapper<SysDictType> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysDictType::getDictCode, dictCode);
        SysDictType sysDictType = baseMapper.selectOne(queryWrapper);
        if (sysDictType == null) {
            return List.of();
        }

        LambdaQueryWrapper<SysDictData> dictDataQueryWrapper = new LambdaQueryWrapper<>();
        dictDataQueryWrapper.eq(SysDictData::getDictTypeId, sysDictType.getId())
                .eq(SysDictData::getStatus, StatusEnum.ENABLE.getCode())
                .orderByAsc(SysDictData::getOrderNum);
        return sysDictDataMapper.selectList(dictDataQueryWrapper);
    }

}
